<?php

namespace app\admin\model;

use app\admin\common\ModelBase;
use service\JsonService;
use service\TreeService;
use service\UtilService;
use think\Db;
use think\Exception;

class User extends ModelBase
{

    protected $update = ["update_time"];


    /**
     * @return mixed
     * @author: hhygyl
     * @name: setUpdateTimeAttr
     * @describe:
     */
    protected function setUpdateTimeAttr()
    {
        return time();
    }



    /**
     * @param array $param
     * @param string $order
     * @author: hhygyl <hhygyl520@qq.com>
     * @name: GetModelDataPage
     * @describe:获取分页数据
     */
    public function GetUsersDataPage($param = [], $order = "id desc")
    {
        $where = [];
        $where['status'] = 1;
        //$where['is_jtzb'] = 1;
        $wherestr = "";
        if(!empty($param)) {
            //搜索条件
            if(!empty($param['name'])) {
                $where['username|mobile'] = ['like', "%".$param['name']."%"];
            }
            if(isset($param['department_id']) && !empty($param['department_id'])){
                $wherestr = "FIND_IN_SET({$param['department_id']},department_ids)";
            }
        }

        try {
            $data = Db::name("user")->where($where)->where($wherestr)->order($order)->page(PAGE)->limit(PERPAGE)->select();
            //dump($data);exit;
            //echo $this->getLastSql();exit;
            $count = Db::name("user")->where($where)->where($wherestr)->count("id");
        } catch (Exception $exception) {

            return JsonService::fail($exception->getMessage());
        }
        return JsonService::result(0, "", $data, $count);

    }


    /**
     * @param $params
     * @return false|int
     * @author: hhygyl
     * @name: editData
     * @describe:修改数据
     */
    public function editData($params)
    {
        $res = $this->isUpdate(true)->allowField(true)->save($params);
        return $res;
    }

}